package com.anbinh.doctor.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anbinh.doctor.GcmRegister;
import com.anbinh.doctor.MainActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.account.FragmentAccount;
import com.anbinh.doctor.fragment.benhnhan.FragmentListPatients;
import com.anbinh.doctor.fragment.lichhen.FragmentLichhen;
import com.anbinh.doctor.fragment.lichkham.FragmentLichkham;
import com.anbinh.doctor.fragment.tinnhan.FragmentMessages;
import com.anbinh.doctor.model.CountNotificaton;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.TabType;
import com.anbinh.doctor.utils.Utils;

public class FragmentMain extends Fragment {
	private View mView;
	private static FragmentMain fragment = null;
	private int mPositionPage = TabType.LICHHEN.getCode();

	private LinearLayout viewtab;
	private ViewPager viewPager;
	private IRcallBak calBak;
	private TextView countNotification;
	private boolean isLoading = false;
	private boolean isRefreshAppointMent = false;
	private boolean isRefreshPatient = false;
	public boolean isRefreshLichkham = false;
	private boolean isRefreshMessage = false;
	private boolean isRefreshDoctor = false;

	public static FragmentMain newInstance() {
		if (fragment == null)
			fragment = new FragmentMain();
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IRcallBak) {
			calBak = (IRcallBak) activity;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		calBak.onDisplay(TabType.LICHHEN.getCode(), null);
		mView = inflater.inflate(R.layout.fragment_main, null);
		return mView;
	}
	@Override
	public void onResume() {
		super.onResume();
		new GcmRegister(getActivity()).newInstance();
	}
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		viewtab = (LinearLayout) mView.findViewById(R.id.viewtab);
		countNotification = (TextView) mView
				.findViewById(R.id.countNotification);
		countNotification.setVisibility(View.GONE);
		List<Fragment> mList = new ArrayList<Fragment>();
		mList.add(FragmentLichhen.newInstance());
		mList.add(FragmentLichkham.newInstance());
		mList.add(FragmentMessages.newInstance());
		mList.add(FragmentListPatients.newInstance());
		mList.add(FragmentAccount.newInstance());

		viewPager = (ViewPager) mView.findViewById(R.id.pager);
		viewPager.setOffscreenPageLimit(5);
		PagerAdapr adapter = new PagerAdapr(getChildFragmentManager(), mList);
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

			}

			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
				mPositionPage = position;
				onTabChanged();
				calBak.onDisplay(mPositionPage, null);
				if (!isLoading) {
					onLoadCountNotification();
					isLoading = true;
				}
				// FragmentLichhen.newInstance().onLoadData();
				/*
				 * FragmentLichkham.newInstance().onResume();
				 * FragmentMessages.newInstance().onResume();
				 * FragmentListPatients.newInstance().onResume();
				 * FragmentAccount.newInstance().onResume();
				 */
				onResetData();
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});

		onTabchangeListener();
		onTabChanged();
	}

	protected void onResetData() {
		if (isRefreshAppointMent) {
			isRefreshAppointMent = false;
			FragmentLichhen.newInstance().onLoadData();
		}
		if (isRefreshLichkham) {
			isRefreshLichkham = false;
			FragmentLichkham.newInstance().onloadData();
		}
		
		if (isRefreshMessage) {
			isRefreshMessage = false;
			FragmentMessages.newInstance().onResetData();
			FragmentMessages.newInstance().onLoadData();
		}

	}

	public void onTabchangeListener() {
		for (int i = 0; i < 5; i++) {
			View line = (View) viewtab.findViewWithTag("tab" + (i + 1));
			line.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (v.getTag().equals("tab" + 1)) {
						viewPager.setCurrentItem(0);
					} else if (v.getTag().equals("tab" + 2)) {
						viewPager.setCurrentItem(1);
					} else if (v.getTag().equals("tab" + 3)) {
						viewPager.setCurrentItem(2);
					} else if (v.getTag().equals("tab" + 4)) {
						viewPager.setCurrentItem(3);
					} else {
						viewPager.setCurrentItem(4);
					}

				}
			});
		}

	}

	public void onTabChanged() {
		for (int i = 0; i < 5; i++) {
			TextView label = (TextView) viewtab.findViewWithTag("label"
					+ (i + 1));
			ImageView icon = (ImageView) viewtab.findViewWithTag("icon"
					+ (i + 1));

			if (i == mPositionPage) {
				icon.setSelected(true);
				label.setSelected(true);
			} else {
				icon.setSelected(false);
				label.setSelected(false);
			}
		}
	}

	public class PagerAdapr extends FragmentStatePagerAdapter {

		public List<Fragment> mList;

		public PagerAdapr(FragmentManager fm, List<Fragment> list) {
			super(fm);
			mList = list;
		}

		@Override
		public Fragment getItem(int postion) {
			return mList.get(postion);
		}

		@Override
		public int getCount() {
			return mList.size();
		}
	}

	public void onLoadCountNotification() {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);

		ServerController.getInstance(getActivity()).requestCountNotification(
				userLogin.getData().getToken(),
				new OnRequest<CountNotificaton>() {

					@Override
					public void onSuccess(CountNotificaton data) {
						if (getActivity() == null)
							return;
						isLoading = false;
						if (data.getResponsecode() >= 0) {
							if (data.getData().getCountNotificaton() == 0) {
								countNotification.setVisibility(View.GONE);
							} else {
								countNotification.setVisibility(View.VISIBLE);
								countNotification.setText(data.getData()
										.getCountNotificaton() + "");
							}
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						isLoading = false;
					}
				});
		// notification/countNotification
	}

	public boolean isRefreshAppointMent() {
		return isRefreshAppointMent;
	}

	public void setRefreshAppointMent(boolean isRefreshAppointMent) {
		this.isRefreshAppointMent = isRefreshAppointMent;
	}

	public boolean isRefreshMessage() {
		return isRefreshMessage;
	}

	public void setRefreshMessage(boolean isRefreshMessage) {
		this.isRefreshMessage = isRefreshMessage;
	}

	public void setRefreshLichkham(boolean isRefreshLichkham) {
		this.isRefreshLichkham = isRefreshLichkham;
	}

	public boolean isRefreshLichkham() {
		return isRefreshLichkham;
	}

	public boolean isRefreshPatient() {
		return isRefreshPatient;
	}

	public void setRefreshPatient(boolean isRefreshPatient) {
		this.isRefreshPatient = isRefreshPatient;
	}

	public boolean isRefreshDoctor() {
		return isRefreshDoctor;
	}

	public void setRefreshDoctor(boolean isRefreshDoctor) {
		this.isRefreshDoctor = isRefreshDoctor;
	}
}
